package com.wanxi.dangdang.controller.back.user;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.common.ResultCode;
import com.wanxi.dangdang.model.dto.PermissionAddDto;
import com.wanxi.dangdang.model.vo.PermissionFindPageVO;
import com.wanxi.dangdang.model.dto.PermissionUpdateIdDto;
import com.wanxi.dangdang.model.pojo.Permission;
import com.wanxi.dangdang.model.vo.PermissionFindByIdVo;
import com.wanxi.dangdang.model.vo.PermissionVo;
import com.wanxi.dangdang.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/8/30 0030
 * @Version: 1.0
 */
@Api(tags={"权限接口层（需要登录）"})
@RestController
@RequestMapping("/back/a/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/checkName")
    @ApiOperation(value="检验权限是否存在", notes="检验是否存在,code 1001表示name已经存在")
    public CommonResult checkName(@ApiParam(value = "输入的名字", required = true) @RequestParam String name){
        String s = permissionService.checkName(name);
        if(s!=null)
            return CommonResult.failure(ResultCode.PARAM_IS_INVALID);
        return CommonResult.success();
    }

    @PostMapping("/add")
    @ApiOperation(value="新增权限", notes="超级管理员添加权限,权限页面添加弹窗")
    public CommonResult add(@ApiParam(value = "新增权限参数", required = true) @RequestBody PermissionAddDto permissionAddDto){
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddDto,permission);
        permissionService.add(permission);
        return CommonResult.success();
    }

    @GetMapping("/findById")
    @ApiOperation(value="查询权限通过id")
    public CommonResult<PermissionFindByIdVo> findById(@ApiParam(value = "权限id", required = true) @RequestParam Long id){
        return CommonResult.success(permissionService.findById(id));
    }

    @PostMapping("/findPage")
    @ApiOperation(value="超级管理员分页查询所有权限")
    public CommonResult<PageInfo<List<PermissionFindPageVO>>> findPage(@ApiParam(value = "模糊查询,page加limit", required = true) @RequestBody PermissionVo permissionVo){
        PageInfo<PermissionFindPageVO> permissions = permissionService.findPage(permissionVo);
        return CommonResult.success(permissions);
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "超级管理根据id删除权限")
    public CommonResult deleteById(@ApiParam(value = "权限id", required = true) @RequestParam("id") Long id){
        permissionService.deleteById(id);
        return CommonResult.success();
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "超级管理修改权限")
    public CommonResult updateById(@RequestBody PermissionUpdateIdDto permissionUpdateIdDto){
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateIdDto,permission);
        permissionService.updateById(permission);
        return CommonResult.success();
    }

    @GetMapping("/findPermissionsByRoleId")
    @ApiOperation(value = "超级管理员通过角色id查询其所有权限")
    public CommonResult<ArrayList<PermissionFindPageVO>> findPermissionsByRoleId(@ApiParam(value = "角色id", required = true)@RequestParam("id") Long id){
        List<PermissionFindPageVO> permissions = permissionService.findPermissionsByRoleId(id);
        return CommonResult.success(permissions);
    }
}
